# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2023/12/19 15:06

from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import StaleElementReferenceException
import time, pytest
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.user_resource_packages_locator import ResourcePackagesLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.usergroup_data import UserGroup
import re
from datetime import datetime


class UserResourcePacksgesPage(Basepage):

    """
        公共方法
    """
    def click_reset(self):
        doc = '点击reset清空'
        time.sleep(2)
        self.wait_elevisble(ResourcePackagesLocator.resource_package_reset, doc)
        self.click_element(ResourcePackagesLocator.resource_package_reset, doc)
        self.wait_web_page_rotate(10)
        self.click_element(ResourcePackagesLocator.resource_package_search, doc)
        self.wait_web_page_rotate(10)

    # 等待页面加载转动
    def wait_web_page_rotate(self, time=100):
        doc = '等待页面加载转动图标'
        try:
            self.wait_elevisble(ResourcePackagesLocator.wait_for_pay, doc, time)
            self.wait_eleinvisble(ResourcePackagesLocator.wait_for_pay, doc, time)
        except:
            pass

    """
    
    """

    # 进入Resource Packsges页面
    def enter_resource_packages_page(self):
        time.sleep(2)
        doc = "进入resource packages菜单页面"
        for i in range(3):
            try:
                self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
                self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        return self.get_element(ResourcePackagesLocator.resource_packages_seccess, doc).text

    # TL637264 | 搜索功能使用正常
    def select_resource_packages_id(self, id='OSS-OPT526867664139542528'):
        doc = "根据resource_packages_id查询"
        for i in range(3):
            try:
                self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
                self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        self.wait_elevisble(ResourcePackagesLocator.resource_package_id, doc)
        self.send_keys(ResourcePackagesLocator.resource_package_id, id, doc)
        self.click_element(ResourcePackagesLocator.resource_package_search, doc)
        try:
            self.wait_elevisble(ResourcePackagesLocator.resource_package_total_items, doc)
            total_items = self.get_element(ResourcePackagesLocator.resource_package_total_items, doc).text
            total = re.findall('\d', total_items)
            print(total)
        except:
            print('数据为空')

    ##TL637267 | Reset功能校验
    def reset_resource_packages_id(self, id='OSS-OPT526867664139542528'):
        doc = "resource_packages中reset功能"
        for i in range(3):
            try:
                self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
                self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        self.click_reset() # 先reset清空
        self.wait_elevisble(ResourcePackagesLocator.resource_package_id, doc)
        self.send_keys(ResourcePackagesLocator.resource_package_id, id, doc)
        self.click_element(ResourcePackagesLocator.resource_package_search, doc)
        value = self.get_element(ResourcePackagesLocator.resource_package_id, doc).get_attribute('value')
        text = '实际值:'+value+' '+'预期值:'+id
        assert value == id,text
        self.wait_elevisble(ResourcePackagesLocator.resource_package_reset, doc)
        self.click_element(ResourcePackagesLocator.resource_package_reset, doc)
        value = self.get_element(ResourcePackagesLocator.resource_package_id, doc).get_attribute('value')
        assert value == ''
        time.sleep(10)
        return value

    # TL637261 | 时间框功能使用正常
    def resource_packages_time(self):
        doc = "时间框选择"
        for i in range(3):
            try:
                self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
                self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        self.wait_elevisble(ResourcePackagesLocator.resource_package_time, doc)
        self.click_element(ResourcePackagesLocator.resource_package_time, doc)
        self.wait_elevisble(ResourcePackagesLocator.resource_package_select_time, doc)
        self.click_element(ResourcePackagesLocator.resource_package_select_time, doc)
        self.click_element(ResourcePackagesLocator.resource_package_select_time, doc)
        self.click_element(ResourcePackagesLocator.resource_package_search, doc)
        time.sleep(5)
        self.wait_elevisble(ResourcePackagesLocator.resource_package_time_begin, doc)
        time_begin = self.get_element(ResourcePackagesLocator.resource_package_time_begin, doc).get_attribute('value')
        time_end = self.get_element(ResourcePackagesLocator.resource_package_time_begin, doc).get_attribute('value')
        print(time_begin,time_end,ResourcePackagesLocator.now_str1)
        assert time_begin == ResourcePackagesLocator.now_str1
        assert time_end == ResourcePackagesLocator.now_str1
        return ResourcePackagesLocator.now_str1

    # 创建资源包 oss_resource
    def test_create_resource_packages(self):
        doc = '创建oss resource'
        self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
        self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
        time.sleep(5)
        try:
            total_items = self.get_element(ResourcePackagesLocator.resource_package_total_items, doc).text
            total = re.findall('\d', total_items)
        except:
            total = 0
        if total != 0:
            self.wait_elevisble(ResourcePackagesLocator.resource_package_create, doc)
            self.move_mouse_to_element_clicklink(ResourcePackagesLocator.resource_package_create,
                                                 'Create OSS Resource Package', doc)
            self.change_windows(doc)
            self.wait_web_page_rotate(30)
            self.wait_elevisble(ResourcePackagesLocator.create_resource_buy_now, doc)
            self.click_element(ResourcePackagesLocator.create_resource_buy_now, doc)
            time.sleep(5)
            self.wait_elevisble(ResourcePackagesLocator.create_resource_package_terms, doc)
            self.move_mouse_to_click_element(ResourcePackagesLocator.create_resource_package_terms, doc)
            self.wait_elevisble(ResourcePackagesLocator.create_resource_pay, doc)
            self.click_element(ResourcePackagesLocator.create_resource_pay, doc)
            self.wait_elevisble(ResourcePackagesLocator.create_resource_check_order, doc)
            self.wait_elevisble(ResourcePackagesLocator.create_resource_check_order, doc)
            self.change_windows(doc, 0)
            a = self.get_element(ResourcePackagesLocator.resource_package_total_items, doc).text
            b = re.findall('\d', a)
            print(b)

    #TL637270 | Enable Auto-renewal功能校验
    def enable_auto_renewal(self):
        doc = 'Enable Auto-renewal功能校验'
        self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
        self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
        time.sleep(5)
        self.click_reset() # 清空
        self.action_right(ResourcePackagesLocator.test1,doc)
        self.wait_elevisble(ResourcePackagesLocator.create_resourc_num1,doc)
        self.move_mouse_to_click_element(ResourcePackagesLocator.create_resourc_num1,doc)
        time.sleep(5)
        self.wait_elevisble(ResourcePackagesLocator.enable_auto, doc)
        text = self.get_element(ResourcePackagesLocator.enable_auto,doc).text
        self.move_mouse_to_click_element(ResourcePackagesLocator.enable_auto, doc)
        time.sleep(5)
        text = self.get_element(ResourcePackagesLocator.if_enable_auto, doc).text
        print(text)
        assert text == 'Y'

        self.wait_elevisble(ResourcePackagesLocator.create_resourc_num1,doc)
        self.move_mouse_to_click_element(ResourcePackagesLocator.create_resourc_num1,doc)
        time.sleep(5)
        self.wait_elevisble(ResourcePackagesLocator.enable_auto, doc)
        text = self.get_element(ResourcePackagesLocator.enable_auto,doc).text
        self.move_mouse_to_click_element(ResourcePackagesLocator.enable_auto, doc)
        time.sleep(5)
        text = self.get_element(ResourcePackagesLocator.if_enable_auto, doc).text
        print(text)
        assert text == 'N'

    # TL707854 | Resource packages-产品展示逻辑优化
    def check_create_resource_packages(self):
        doc = '检查Resource packages-产品展示逻辑优化'
        self.wait_elevisble(ResourcePackagesLocator.resource_packages_tab, doc)
        self.click_element(ResourcePackagesLocator.resource_packages_tab, doc)
        time.sleep(5)
        self.wait_elevisble(ResourcePackagesLocator.move_create_resource_package, doc)
        self.move_mouse_to_element(ResourcePackagesLocator.move_create_resource_package)
        self.wait_elevisble(ResourcePackagesLocator.lick_detil(self,'Create OSS Resource Package'),doc)
        assert self.get_element(ResourcePackagesLocator.lick_detil(self,'Create OSS Resource Package'),doc).text == 'Create OSS Resource Package'
        self.wait_elevisble(ResourcePackagesLocator.lick_detil(self,'Create NAS Resource Package'), doc)
        assert self.get_element(ResourcePackagesLocator.lick_detil(self,'Create NAS Resource Package'),
                                doc).text == 'Create NAS Resource Package'
        self.wait_elevisble(ResourcePackagesLocator.lick_detil(self,'Create WAF Resource Package'), doc)
        assert self.get_element(ResourcePackagesLocator.lick_detil(self,'Create WAF Resource Package'),
                                doc).text == 'Create WAF Resource Package'